#ifndef _SPI_H_
#define _SPI_H_
#include "air32f10x_conf.h"

/* 单字节读写 */
__STATIC_FORCEINLINE void spi1_byte_write(uint8_t spi_data)
{
    (void)SPI1->DR;
    SPI1->DR = spi_data;
    while ((SPI1->SR & SPI_I2S_FLAG_RXNE) == 0)
    {
    }
}

__STATIC_FORCEINLINE uint8_t spi1_byte_read()
{
    (void)SPI1->DR;
    SPI1->DR = 0x00;
    while ((SPI1->SR & SPI_I2S_FLAG_RXNE) == 0)
    {
    }
    return SPI1->DR;
}

__STATIC_FORCEINLINE void spi1_read(uint8_t *read_buf, uint8_t len)
{
    while (len--)
    {
        *read_buf++ = spi1_byte_read();
    }
}

/* 多字节读写 */
__STATIC_FORCEINLINE void spi1_write(uint8_t *write_buf, uint8_t len)
{
    while (len--)
    {
        spi1_byte_write(*write_buf);
        write_buf++;
    }
}

void spi1_init();

#endif
